<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use App\Models\AdminUser;
use App\Models\ProjectUser;
use App\Models\Project;
use Hash;
use Log;

class UserController extends Controller{
    //获取项目成员 adminid
    public function Members(Request $request){
        $info = $request->all();
        $user_id = getAdminId($info['adminId']);
        // $body['resultBody'] = Db::table('admin_users')->where(['pid'=>$user_id])->pluck('id as value', 'name');
        $body['resultBody'] = Db::table('admin_users')->where(['pid'=>$user_id])->select('id as value', 'name')->get()->toArray();
    //  print_r($list);die;
     // $res = array_combine(array_column($list, 'id'), $list);
     //    foreach ($res as $key => &$value) {
     //        $res[$key] = $value['name'];
     //    }
        // print_r($body);die;
        return $body;
    }

    public function getMembers(Request $request){
        $info = $request->all();
        $arr['user'] = AdminUser::where(['id'=>$info['user_id']])->first()->toArray();
        // $user_role = Db::table('admin_role_users')->where(['user_id'=>$info['user_id']])->first();
        // $role = Db::table('admin_role_users')->where(['user_id'=>$info['adminId']])->first();
        // switch ($role->role_id) {
        //     case 1://创建人
        //         $arr['role'] = array(array('role_id'=>0,'rolename'=>'请选择角色'),array('5'=>'超级管理员'));
        //         break;
        //     case 4://主管
        //         $arr['role'] = array(array('0'=>'请选择角色'),array('6'=>'操作员'),array('8'=>'工程师'));
        //         break;
        //     case 5://超级管理员
        //         $arr['role'] = array(array('role_id'=>0,'rolename'=>'请选择角色'),array('role_id'=>6,'rolename'=>'操作员'),array('role_id'=>8,'rolename'=>'工程师'));
        //         break;
        //     default:
        //         $arr['role'] = array(array('0'=>'无可创建角色'));
        //         break;
        // }
        $admin_project_list = ProjectUser::where(['user_id'=>$info['adminId']])->pluck('projectId')->toArray();
        $arr['project_list'] = ProjectUser::where(['user_id'=>$info['user_id']])->pluck('projectId');
        $arr['project'] = Project::whereIn('projectId',$admin_project_list)->select('projectname','projectId')->get()->toArray();
        // foreach ($arr['project'] as &$val){
        //     foreach ($arr['project_list'] as $val1){
        //         if($val['projectId'] == $val1['projectId']){
        //             $val['checked'] = 'true';
        //         }
        //     }
        // }
        return json_encode($arr);
    }
    //修改成员
    public function editMember(Request $request){
        $info = $request->all();
        if(isset($info['project'])){
            $project = explode(',', $info['project']);
            ProjectUser::where(['user_id'=>$info['user_id']])->delete();
            foreach ($project as $val){
                $res = Db::table('xfy_project_users')->insert(['projectId' => $val, 'user_id' => $info['user_id'],'created_at' => date("Y-m-d H:i:s")]);
            }
        }
        if($res){
            // $res = Db::table('admin_users')->insertGetId($info);
            // foreach ($project as $val){
            //     Db::table('admin_role_users')->insert(['role_id' => $val, 'user_id' => $res,'created_at' => date("Y-m-d H:i:s")]);
            // }
            $result['resultCode'] = 1;
            $result['resultMsg'] = '修改成功';
        }else{
            $result['resultCode'] = 0;
            $result['resultMsg'] = '修改失败';
        }
        return $result;
    }
    public function addMember(Request $request){
        $info = $request->all();
        $user = Db::table('admin_users')->where(['username'=>$info['username']])->first();
        $info['created_at'] = date("Y-m-d H:i:s");
        $info['password'] = Hash::make($info['password']);
        if($info['role'] == 6||$info['role'] ==8){
            $info['pid'] = $info['adminId'];
            unset($info['adminId']);
        }
        $project = explode(',', $info['project']);
        $role = $info['role'];
        unset($info['project']);
        unset($info['role']);
        if(!$user){
            $res = Db::table('admin_users')->insertGetId($info);
            Db::table('admin_role_users')->insert(['role_id' => $role, 'user_id' => $res,'created_at' => date("Y-m-d H:i:s")]);
            if(isset($project)){
                foreach ($project as $val){
                    Db::table('xfy_project_users')->insert(['projectId' => $val, 'user_id' => $res,'created_at' => date("Y-m-d H:i:s")]);
                }
            }
            $result['resultCode'] = 1;
            $result['resultMsg'] = '添加成功';
        }else{
            $result['resultCode'] = 0;
            $result['resultMsg'] = '账号已存在';
        }
        return $result;
    }

    public function userlist(Request $request){
        $info = $request->all();
        $user = Db::table('admin_users')->where(['admin_users.pid'=>$info['adminId']])
        ->join('admin_role_users','admin_users.id','=','admin_role_users.user_id')
        ->join('admin_roles','admin_role_users.role_id','=','admin_roles.id')
        ->select('admin_users.*', 'admin_roles.name as rolesname')->get()->toArray();
        if($user){
            $result['resultBody'] = $user;
            $result['resultCode'] = 1;
        }else{
            $result['resultMsg'] = '暂无成员';
            $result['resultCode'] = 0;
        }
        return $result;
    }
    //获取可创建角色
    public function getRole(Request $request){
        $info = $request->all();
        $role = Db::table('admin_role_users')->where(['user_id'=>$info['adminId']])->first();
        switch ($role->role_id) {
            case 1://创建人
                $arr['role'] = array('0'=>'请选择角色','5'=>'超级管理员');
                break;
            case 4://主管
                $arr['role'] = array('0'=>'请选择角色','6'=>'操作员','8'=>'工程师');
                break;
            case 5://超级管理员
                $arr['role'] = array(array('role_id'=>0,'rolename'=>'请选择角色',),array('role_id'=>6,'rolename'=>'操作员'),array('role_id'=>8,'rolename'=>'工程师'));
                break;
            default:
                $arr['role'] = array('0'=>'无可创建角色');
                break;
        }
        $project_list = ProjectUser::where(['user_id'=>$info['adminId']])->pluck('projectId')->toArray();
        $arr['project'] = Project::whereIn('projectId',$project_list)->select('projectname','projectId')->get()->toArray();
        return json_encode($arr);
    }

    //修改密码
    public function changePass(Request $request){
        $info = $request->all();
        $users = Db::table('admin_users')->where(['id'=>$info['adminId']])->first();
        // print_r(Hash::check($info['newpassword'],$info['repassword']));die;
        if (Hash::check($info['oldpass'],$users['password'])) {
            $password = Hash::make($info['newpassword']);
            Db::table('admin_users')->where(['id'=>$info['adminId']])->update(['password'=>$password]);
            $body['resultCode'] =1;
            $body['resultMsg'] = '修改成功';
        }else{
            $body['resultCode'] =0;
            $body['resultMsg'] = '修改失败';
        }
        return $body;
    }
    //删除用户
    public function deleteUser(Request $request){
        $info = $request->all();
        Db::table('admin_users')->where(['username'=>$info['username']])->first();
    }
}